package 力扣._00周赛._424周赛.问题2;

class Solution {
    public boolean isZeroArray(int[] nums, int[][] queries) {
        int n = nums.length;
        int[] diff = new int[n + 1];

        // 使用差分数组记录每个查询的影响
        for (int[] query : queries) {
            int l = query[0];
            int r = query[1];
            diff[l]++;
            if (r + 1 < n) {
                diff[r + 1]--;
            }
        }

        // 计算每个位置的覆盖次数
        int[] count = new int[n];
        count[0] = diff[0];
        for (int i = 1; i < n; i++) {
            count[i] = count[i - 1] + diff[i];
        }

        // 检查是否可以将 nums 转换为零数组
        for (int i = 0; i < n; i++) {
            if (nums[i] > count[i]) {
                return false;
            }
        }

        return true;

    }

}